<?php
namespace Admin\Controller;
use Think\Controller;

class QuestionsController extends HomeController {

	/*
	* Function:		questionsList
	* Description:	提问列表
	*/
    public function questionsList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter['flag'] = 1;
		$filter = returnFilter($filter);
		$list = D('Questions')->getQuestionsList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
			foreach($list as $key=>$val){
				if(!empty($val['user_id'])){
					$userInfo = D('User')->getUserInfo($val['user_id']);
					$list[$key]['username'] = $userInfo['username'];
				}
			}
		}
		$count =  D('Questions')->getQuestionsCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$statusList = [['id'=>0,'name'=>'待审核'],['id'=>1,'name'=>'已审核'],['id'=>2,'name'=>'审核失败']];
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->assign('statusList',$statusList);
		$this->display();
    }
    
	/*
	* Function:		auitQuestions
	* Description:	审核提问
	*/
    public function auitQuestions($id = ''){
		if(IS_POST){
			$arr = array();
			$data = I('post.');
			if(!$info = M('questions')->where(['id'=>$data['id']])->find()){
			    $ret = array('code'=>0,'message'=>'不存在此用户提问记录!');
				$this->ajaxReturn($ret);
			}
			if($data['auitSelect']==1){
			    $arr['status'] = 1;
				$arr['op_memo'] = '';
			}else if($data['auitSelect']==2){
				$arr['status'] = 2;
				if(empty($data['reason'])){
					$ret = array('code'=>0,'message'=>'审核不通过原因不能为空!');
					$this->ajaxReturn($ret);
				}
				$arr['reason'] = $data['reason'];
			}
			$ret = M('questions')->where(['id'=>$data['id']])->save($arr);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'提交成功!');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'提交失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->assign('id',$id);
			$info = M('questions')->where(['id'=>$id])->find();
			$this->assign('info',$info);
			$userInfo = D('User')->getUserInfo($info['user_id']);
			$this->assign('userInfo',$userInfo);
			$auitSelectList = array(array('value'=>1,'name'=>'通过'),array('value'=>2,'name'=>'不通过'));
			$this->assign('auitSelectList',$auitSelectList);
			$this->display();
		}
	}
	
	/*
	* Function:		deleteQuestions
	* Description:	删除提问
	* @param:		$id
	*/
	public function deleteQuestions($id = ''){
		if(M('questions')->where(['id'=>$id])->delete()){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
	

	/*
	* Function:		answerQuestions
	* Description:	回答提问
	*/
    public function answerQuestions($id = ''){
		if(IS_POST){
			$arr = array();
			$data = I('post.');
			$arr['tid'] = $data['id'];
			if(!$info = M('questions')->where(['id'=>$id])->find()){
				$ret = array('code'=>0,'message'=>'不存在此条提问!');
				$this->ajaxReturn($ret);
			}
			$arr['user_id'] = $info['user_id'];
			$arr['content'] = $data['content'];
			$arr['op_id'] = session(C('USER_AUTH_KEY'));
			$arr['addtime'] = date('Y-m-d H:i:s',time());
			$ret = M('question_list')->where(['id'=>$data['id']])->add($arr);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'提交成功!');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'提交失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->assign('id',$id);
			$info = M('questions')->where(['id'=>$id])->find();
			$this->assign('info',$info);
			$userInfo = D('User')->getUserInfo($info['user_id']);
			$this->assign('userInfo',$userInfo);
			$list = M('question_list')->where(['tid'=>$id,'status'=>1])->select();
			if(is_array($list)&&count($list)>0){
				foreach($list as &$vo){
					$opInfo = M('operators')->where(['id'=>$vo['op_id']])->find();
					$vo['op_name'] = $opInfo['login_name'];
				}
			}
			$this->assign('list',$list);
			$this->display();
		}
	}
}